<?php
namespace Admin\Controller;
use Common\Model\AdminModel;
use Common\Model\AdminObject;
use Common\Model\ConsumerModel;
use Think\Model;
use Think\Controller;
class IndexController extends Controller {
    public function index(){
        $this->display();
    }
    public function login_action(){
        $admin=D('Admin');
        $result=$admin->fetchPassword($_POST['username']);
        if($result==null)
            $this->error('用户不存在','Index');
        else if($result!=$_POST['password'])
            $this->error('密码不正确','Index');
        else
        {
            $_SESSION['id']=$_POST['username'];
            $this->success('密码正确','zhuye');
        }
   }
    public function zhuye(){
        if($_SESSION['id']==null)
            $this->error('未登录','Index');
        $this->assign('admin',$_SESSION['id']);
        $this->display();
    }
    public function user(){
        if($_SESSION['id']==null)
            $this->error('未登录','Index');
        $data=D('Admin');
        $data=$data->userPage();
        $this->assign($data);
        $this->display();
    }
    public function deleuser(){
        if($_SESSION['id']==null)
            $this->error('未登录','Index');
        $result=D('Consumer');
        if($result->deleConsumer($_GET['id']))
            $this->success('删除成功','/Index/user');
        else
            $this->error('删除失败','/Index/user');
    }
    public function addUser(){
        if($_SESSION['id']==null)
            $this->error('未登录','Index');
        $this->display();
    }
    public function addUser_action(){
        if($_SESSION['id']==null)
            $this->error('未登录','Index');
        $consumer=D('Consumer');
        $data['Username']=$_POST['username'];
        $data['Password']=$_POST['password'];
        $result=$consumer->fetchPassword($_POST['username']);
        if($result)
            $this->error('用户已存在','/Index/addUser');
        else{
            $consumer->addConsumer($data);
            $this->success('保存成功','/Index/user');
        }
//        $this->display();
    }
    public function goods(){
        if($_SESSION['id']==null)
            $this->error('未登录','Index');
        $this->display();
    }
    public function GoodsFenAjax(){
        if($_SESSION['id']==null)
            $this->error('未登录','Index');
        $value= I('post.fen');
        $result=D('goods');
        $data = $result->where("category = $value")->select();
        return $this->ajaxReturn($data);
    }
    public function addGoods(){
        if($_SESSION['id']==null)
            $this->error('未登录','Index');
        $this->display();
    }
    public function addGoods_action(){
        if($_SESSION['id']==null)
            $this->error('未登录','Index');
        move_uploaded_file($_FILES['address']['tmp_name'],'./Public/Home/images/'. $_FILES['address']['name']);
        $filename = './Public/Home/images/'. $_FILES['address']['name'];
        chmod($filename, 0655);
        $data['Name']=$_POST['name'];
        $data['Price']=$_POST['price'];
        $data['Edition']=$_POST['edition'];
        $data['content']=$_POST['content'];
        $data['Color']=$_POST['color'];
        $data['Category']=$_POST['fen'];
        $data['address']='/Public/Home/images/'.$_FILES['address']['name'];
        $result=M('Goods');
        if($result->add($data))
            $this->success('插入成功','/Index/goods');
        else
            $this->error('插入出错','/Index/addGoods');
    }
    public function inGoods(){
        if($_SESSION['id']==null)
            $this->error('未登录','Index');
        $this->display();
    }
    public function inGoodsFenAjax(){
        if($_SESSION['id']==null)
            $this->error('未登录','Index');
        $value= I('post.fen');
        $result=D('goods');
        $datas = $result->where("category = $value && state = 0")->select();

        foreach($datas as $key => $data) {
            $datas[$key]['url'] = U('Index/inGoods_action',array('id'=> $data['id']));
        }
        return $this->ajaxReturn($datas);
    }
    public function inGoods_action(){
        if($_SESSION['id']==null)
            $this->error('未登录','Index');
        $result= M('Goods');
        $data['State'] = '1';
        if($result->where('ID='.$_GET['id'])->save($data))
            $this->success('上架成功','/Index/inGoods');
        else
            $this->error('上架失败','/Index/inGoods');
    }
    public function outGoods(){
        if($_SESSION['id']==null)
            $this->error('未登录','Index');
        $this->display();
    }
    public function outGoodsFenAjax(){
        if($_SESSION['id']==null)
            $this->error('未登录','Index');
        $value= I('post.fen');
        $result=D('goods');
        $datas = $result->where("category = $value && state = 1")->select();

        foreach($datas as $key => $data) {
            $datas[$key]['url'] = U('Index/outGoods_action',array('id'=> $data['id']));
        }
        return $this->ajaxReturn($datas);
    }
    public function outGoods_action(){
        if($_SESSION['id']==null)
            $this->error('未登录','Index');
        $result= M('Goods');
        $data['State'] = '0';
        if($result->where('ID='.$_GET['id'])->save($data))
            $this->success('下架成功','/Index/outGoods');
        else
            $this->error('下架失败','/Index/outGoods');
    }
    public function order(){
        if($_SESSION['id']==null)
            $this->error('未登录','Index');
        $data=D('Order');
        $data=$data->userPage();
//        foreach($data as $value)
//        {
//            dump($value);
//        }
//        die();
        foreach($data['list'] as $key => $vo)
        {
            $result=D('Consumer');
            $result->query($vo['consumerid']);
            $data['list'][$key]["consumerid"]=$result->username;
        }
        $this->assign($data);
        $this->display();
    }
    public function AuditingOrder(){
        if($_SESSION['id']==null)
            $this->error('未登录','Index');
        $data=D('Order');
        $data=$data->auditingPage();
        if($data['list']){
             $result=D('Consumer');
            $result->query($data['list'][0]['consumerid']);
            $data['list'][0]['consumerid']=$result->username;
            $this->assign($data);
        }
        $this->display();
    }
    public function  auditingorder_action(){
        if($_SESSION['id']==null)
            $this->error('未登录','Index');
        $result= M('order');
        $data['State'] = $_GET['state'];
        if($result->where('ID='.$_GET['id'])->save($data))
            $this->success('操作成功','/Index/AuditingOrder');
        else
            $this->error('操作失败','/Index/AuditingOrder');
    }

}